package cn.tedu.egaddress1.controller;

import cn.tedu.egaddress1.base.response.JsonResult;
import cn.tedu.egaddress1.base.response.StatusCode;
import cn.tedu.egaddress1.mapper.AddressMapper;
import cn.tedu.egaddress1.pojo.datatransferobject.AddressAddParam;
import cn.tedu.egaddress1.pojo.entity.Address;
import cn.tedu.egaddress1.pojo.valueobject.AddressInfoVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static cn.tedu.egaddress1.base.response.JsonResult.alRight;

@Slf4j
@RestController
@RequestMapping("/v1/address")
public class AddressController {
    @Autowired
    private AddressMapper addressMapper;

    @PostMapping("/add")
    public JsonResult addAddressParam(@RequestBody AddressAddParam addressAddParam) {
        log.debug("addressAddParam:" + addressAddParam);
        Address address = new Address();
        BeanUtils.copyProperties(addressAddParam, address);
        int num = addressMapper.insertAddress(address);
        if (num > 0) {
            return JsonResult.alRight();
        } else {
            return new JsonResult(StatusCode.OPERATION_FAILED);
        }
    }

    @GetMapping("/list")
    public JsonResult selectMessage() {
        List<AddressInfoVO> addressInfoVOS = addressMapper.selectAllMessage();
        return JsonResult.alRight(addressInfoVOS);
    }

    @PostMapping("/del")
    public JsonResult deleteSomething(Integer id) {
        int num = addressMapper.deleteAddress(id);
        if (num > 0) {
            return JsonResult.alRight();
        } else {
            return new JsonResult(StatusCode.OPERATION_FAILED);
        }
    }
}
